import re
import requests
import time

url = "https://www.mmonly.cc/tag/jkzf/"
headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36"}
resp = requests.get(url, headers=headers)
resp.encoding = "gb2312"
all_code = resp.text
# print(all_code)

regular1 = re.compile(r'class="ABox"><a target="_blank" href="(?P<newurl>.*?)">', re.S)
regular2 = re.compile(r"次</span></a></li>.*?href='(?P<picture>.*?)'>查看原图</a></li>", re.S)
newurl = regular1.finditer(all_code)
count = 0
for i in newurl:
    # print(i.group("newurl"))
    # for j in i.group("newurl"):
    resp1 = requests.get(i.group("newurl"))
    resp1.encoding = "gbk"
    # print(resp1.text)
    all_code2 = resp1.text
    result = regular2.finditer(all_code2)
    # result=result[1:-2]
    for jj in result:
        print(jj.group("picture"))

        # img_name=str(count)+".jpg"

        picture = requests.get(jj.group("picture"))
        # with open("/Users/shenduxuexi/Desktop/jk"+img_name,mode="wb") as f:

        # 保存路径
        string = 'E:/IDEA/myPY/myTest1/jk/' + str(count) + '.jpg'
        fp = open(string, 'wb')
        fp.write(picture.content)
        fp.close()
        print("下载成功")
        count += 1

        time.sleep(1)
resp.close()
print("jk小姐姐已全部收入囊中")
